/**
 * 
 */
package org.t2.game.server.module.data.service.parser;

import java.util.List;

import org.gaming.tool.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.t2.game.server.module.data.model.Data;
import org.t2.game.server.module.data.saver.PlayerLevelSaver;
import org.t2.game.server.module.data.saver.PlayerMissionSaver;
import org.t2.game.server.module.data.service.DataParserService;
import org.t2.game.server.module.data.struct.PlayerData;
import org.t2.game.server.module.player.model.Player;
import org.t2.game.server.module.player.service.PlayerService;

/**
 * @author YY
 */
@Component
public class PlayerParserService extends DataParserService {

	@Autowired
	private PlayerService playerService;
	@Autowired
	private PlayerLevelSaver playerLevelSaver;
	@Autowired
	private PlayerMissionSaver playerMissionSaver;
	
	@Override
	public String dataType() {
		return "player";
	}

	@Override
	public void parser(long playerId, List<Data> datas, String reason) {
		Player player = playerService.getEntity(playerId);
		
		boolean needUpdate = false;
		for(Data data : datas) {
			PlayerData playerData = GsonUtil.parseJson(data.getPlaintext(), PlayerData.class);
			
			if(playerData.getAccount() != null && playerData.getAccount().getAccountLevel() != player.getLevel()) {
				playerLevelSaver.setNumber(player, reason, playerData.getAccount().getAccountLevel());
				needUpdate = true;
			}
			if(playerData.getVip() != null && playerData.getVip().getVipLevel() != player.getVipLevel()) {
				player.setVipLevel(player.getVipLevel());
				needUpdate = true;
			}
			if(playerData.getPlayerIconId() != player.getHead()) {
				player.setHead(playerData.getPlayerIconId());
				needUpdate = true;
			}
			if(playerData.getFirstclearance() != player.getMission()) {
				playerMissionSaver.setNumber(player, reason, playerData.getFirstclearance());
				needUpdate = true;
			}
		}
		if(needUpdate) {
			playerService.update(player);
		}
	}
	
}
